package com.nowcoder.community.util;

import ch.qos.logback.core.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class CommunityUtils {
    public static String generateUUID() {
        return UUID.randomUUID().toString().replace("-","");
    }

    public static String md5(String key) {
        if (StringUtil.isNullOrEmpty(key)) {
            return null;
        }
        return DigestUtils.md5DigestAsHex(key.getBytes());
    }

    public static String getJsonString(int code, String msg, Map<String,Object> map) {
        JSONObject json =new JSONObject();
        json.put("code",code);
        json.put("msg",msg);
        if (map!=null) {
            for (String key:map.keySet()) {
                json.put(key,map.get(key));
            }
        }
        return json.toJSONString();
    }

    public static String getJsonString(int code, Map<String,Object> map) {
        JSONObject json =new JSONObject();
        json.put("code",code);
        if (map!=null) {
            for (String key:map.keySet()) {
                json.put(key,map.get(key));
            }
        }
        return json.toJSONString();
    }

    public static String getJsonString(int code, String msg) {
        JSONObject json =new JSONObject();
        json.put("code",code);
        json.put("msg",msg);
        return json.toJSONString();
    }

    public static String getJsonString(int code) {
        JSONObject json =new JSONObject();
        json.put("code",code);
        return json.toJSONString();
    }

    public static void main(String[] args) {
        Map<String,Object> map =new HashMap<>();
        map.put("name","樊旭");
        map.put("age",18);
        System.out.println(getJsonString(0,"okk",map));
    }


}
